package com.app.couponsharelife.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle

import com.hwangjr.rxbus.RxBus
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.utils.ToastUtil
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory

class WXPayEntryActivity : Activity(), IWXAPIEventHandler {

    private var api: IWXAPI? = null

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Contacts.WECHAT_APP_ID)
        api!!.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        api!!.handleIntent(intent, this)
    }

    override fun onReq(req: BaseReq) {
        //Log.e("eeee","---->${req.type}")
    }

    override fun onResp(resp: BaseResp) {
        //Log.e("eeee","---->${resp.type}")
        if (resp.type == ConstantsAPI.COMMAND_PAY_BY_WX) {
            when (resp.errCode) {
                0 -> RxBus.get().post("wechatrepay", "wechatrepay")
                -1 -> showToastMsg("支付失败")
                -2 -> showToastMsg("取消支付")
            }
        }else if (resp.type == ConstantsAPI.COMMAND_ADD_CARD_TO_EX_CARD_PACKAGE){
            when (resp.errCode) {
                0 -> RxBus.get().post("wechatcard", "")
                -1 -> showToastMsg("领取失败")
                -2 -> showToastMsg("取消领取")
            }
        }
        finish()
    }

    fun showToastMsg(msg: String) {
        ToastUtil.getInstance().showToast(msg)
    }

    companion object {

        private val TAG = "WXPayEntryActivity"
    }
}